package com.yonginitagmaker.atv;

import java.nio.charset.Charset;

import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;

import com.yonginitagmaker.R;
import com.yonginitagmaker.util.FormatUtil;
import com.yonginitagmaker.util.LogUtil;

public class AtvTagResult extends Activity
{
	private String		TAG			= "BRYAN";

	private TextView	mTxtLog	= null;
	private TextView	mTxtDone	= null;
	
	private NdefMessage[]	msgs		= null;
	
	private final Charset		CHART_SET		= Charset.forName("US-ASCII");

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.atv_show);

		mTxtLog = (TextView) findViewById(R.id.txtLog);
		mTxtDone = (TextView) findViewById(R.id.txtDone);
		
		Tag tagFromIntent = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
		LogUtil.e("tag null ? " + (tagFromIntent == null));
		
		String[] techList = tagFromIntent.getTechList();
		for(int i = 0 ; i < techList.length; i++)
		{
			LogUtil.e("techList("+i+") : " + techList[i]);
		}
		
		if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()))
		{
			Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
			if(rawMsgs != null)
			{
				msgs = new NdefMessage[rawMsgs.length];
				for (int i = 0; i < rawMsgs.length; i++)
				{
					msgs[i] = (NdefMessage) rawMsgs[i];
				}
			}
			
			String log = "";
			
			if(msgs != null && msgs.length > 0)
			{
				for(int j = 0; j < msgs.length; j ++)
				{
					NdefRecord[] record = msgs[j].getRecords();
					for (int i = 0; i < record.length; i++)
					{
						String tnf = record[i].getTnf() + "";
						String id = new String(record[i].getId(), CHART_SET);
						String type = new String(record[i].getType(), CHART_SET);
						String payLoad = new String(record[i].getPayload(), CHART_SET);
						
						LogUtil.e("record[" + i + "] : " + record[i]);
						LogUtil.e("record[" + i + "].getTnf : " + tnf);
						LogUtil.e("record[" + i + "].getId : " + id);
						LogUtil.e("record[" + i + "].getType : " + type);
						LogUtil.e("record[" + i + "].getPayload : " + payLoad);
						
						log += "record[" + i + "].getTnf : " + tnf + "\n";
						log += "record[" + i + "].getId : " + id + "\n";
						log += "record[" + i + "].getType : " + type + "\n";
						log += "record[" + i + "].getPayload : " + payLoad + "\n";
						
						if(!FormatUtil.isNullorEmpty(payLoad))
							mTxtDone.setText(payLoad);
					}
				}
				
				mTxtLog.setText(log);
			}
		}
		
//		String str = readTag(tagFromIntent);
//		if(FormatUtil.isNullorEmpty(str))
//		{
//			LogUtil.e("str null");
//		}
//		else
//		{
//			LogUtil.e("str : " + str);
//		}

//		mTxtDone.setText(str);
	}

//	public String readTag(Tag tag)
//	{
//		MifareUltralight mifare = MifareUltralight.get(tag);
//		try
//		{
//			mifare.connect();
//			byte[] payload = mifare.readPages(4);
//			LogUtil.e("payload null ? " + (payload == null));
//			return new String(payload, Charset.forName("US-ASCII"));
//		}
//		catch (IOException e)
//		{
//			Log.e(TAG, "IOException while writing MifareUltralight message...", e);
//		}
//		finally
//		{
//			if(mifare != null)
//			{
//				try
//				{
//					mifare.close();
//				}
//				catch (IOException e)
//				{
//					Log.e(TAG, "Error closing tag...", e);
//				}
//			}
//		}
//		return null;
//	}
}
